package com.cjst.controller.part;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.pojo.part.PartType;
import com.cjst.pojo.part.PtypeRelevance;
import com.cjst.pojo.sys.SysMenu;
import com.cjst.pojo.sys.SysRelation;
import com.cjst.pojo.sys.SysRole;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.part.PartTypeService;
import com.cjst.service.part.PtypeRelevanceService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("/part/ptypeRelevance")
public class PtypeRelevanceController extends BaseController {
	
	@Autowired
	private PtypeRelevanceService ptypeRelevanceService;
	
	
	@Autowired
	private PartTypeService partTypeService;
	@RequestMapping("")
    @RequiresPermissions("role")
    public String index() {
    	
        return "system/role/role";
    }
	
	  /**
     * 角色数据
     * @param roleId
     * @param model
     * @return
     * @throws Exception
     */					 
    @RequestMapping(value = "/toRelevanceAssign/{partsId}")
    @RequiresPermissions("parts_relevance")
    public String roleAssign(@PathVariable("partsId") String partsId, Model model) throws Exception {
        if (StringUtils.isEmpty(partsId)) {
            throw new Exception("参数传递为空");
        }
        model.addAttribute("partsId", partsId);
        PtypeRelevance pr = ptypeRelevanceService.get(partsId);
        if (pr != null) {
            model.addAttribute("roleName", pr.getPartId());
        }
        return "part/parts/parts_relevance";
    }
    
    
    
    
    /**
     * 关联
     * @param roleId
     * @return
     * @throws Exception
     */   
    @RequestMapping(value = "/menuTreeListByPartsId/{partsId}")
    @ResponseBody
    public List<ZTreeNode> roleTreeListByUserId(@PathVariable String partsId) throws Exception {
        if (StringUtils.isEmpty(partsId)) {
            throw new Exception("参数传递为空，查询失败");
        }
        WherePrams wherePrams = Method.where("partId", C.EQ, partsId);
        List<PtypeRelevance> relevances = ptypeRelevanceService.list(wherePrams);
        //获取拥有的菜单id
        List<String> menuIds = new ArrayList<>();
        for (PtypeRelevance relevance : relevances) {
            menuIds.add(relevance.getTypeId());
        }
        WherePrams partTypePrams = Method.where("delFlag", C.EQ, "0");
        //查询整个菜单列表
        List<PartType> partTypes = partTypeService.list(partTypePrams);

        List<ZTreeNode> nodes = new ArrayList<>();
        //遍历menus
        for (PartType partType : partTypes) {
            ZTreeNode node = new ZTreeNode();
            node.setId(partType.getId());
            node.setpId(partType.getPtypeParent());
            node.setName(partType.getPtypeName());
            if ("0".equals(partType.getPtypeParent())) {
                node.setIsOpen(true);
            } else {
                node.setIsOpen(false);
            }
            //判断menusId是否为空,查询menus哪些是在menusId中，并给出标记
            if (menuIds.size() > 0 && menuIds.contains(partType.getId())) {
                node.setChecked(true);
            } else {
                node.setChecked(false);
            }
            nodes.add(node);
        }
        return nodes;
    }

    /**
     * 配置权限
     */
    @RequestMapping("/setRelevance")
    @ResponseBody
    @RequiresPermissions("parts_relevance")
    public Object setAuthority(@RequestParam("partsId") String partsId,
        @RequestParam("ids") String ids) throws Exception {
        if (StringUtils.isEmpty(partsId)) {
            return new ResultObj(1, "参数传递为空，权限配置失败", null);
        }
        
       ptypeRelevanceService.setRelevance(partsId, ids);
        
        return new ResultObj(0, "成功", null);
    }
    
    
    
  

}
